(function(){

	if (!window.CJ) {
		window.CJ = {};
	}
	if (!window.CJ.plugin) {
		window.CJ.plugin = {};
	}

	CJ.plugin.membership = {
		init : function () {
			this.DeviceAPI = document.getElementById('service');
		},
		isLogin : function () {
			try{
				var resultVal = this.DeviceAPI.isSignedInAccount();
				return resultVal;
			} catch  (err) {
				console.log(err.message);
				if (!CJ.IS_TV) {
					return true;
				}
			}
		},
		login : function (success, error) {
			try{
				var self = this;
				this.DeviceAPI.onRequestLogin = function (e) {
					if ( e.result ) {
						self.userID = e.userID;
						success(self.userID);
					} else {
						error();
					}
					CJ.plugin.Player.rePlay();
				};
				CJ.plugin.Player.removeVideo();
				this.DeviceAPI.requestLogin();
			} catch  (err) {
				console.log(err.message);
			}
		},
		logout : function () {
			try{
				var resultVal = this.DeviceAPI.isSignedInAccount();
				if (resultVal) {
					this.DeviceAPI.logoutAccount();
				}
			} catch  (err) {
				console.log(err.message);
			}
		},
		confirmUser : function (success, error) {
			try{
				var self = this;
				this.DeviceAPI.onRequestConfirmUser = function (e) {
					if ( e.result ) {
						self.userID = e.userID;
						success(self.userID);
					} else {
						error();
					}
				};
				this.DeviceAPI.requestConfirmUser("ADULT");
			} catch  (err) {
				console.log(err.message);
			}
		},
		getUserID: function () {
			try{
				var ret = this.DeviceAPI.getUserID();
				if ( ret.result ) {
					this.userID = ret.userID;
					return this.userID;
				} else {
					return null;
				}
			} catch  (err) {
				console.log(err.message);
				if (!CJ.IS_TV) {
					return "1";
				}
			}
		}
	}
	
	$(document).ready(function(){
		CJ.plugin.membership.init();
	});

})();